package org.Vancl.Dao.ImpI;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.Vancl.Dao.LoginDao;
import org.Vancl.entity.Login;
import org.Vancl.entity.User;
import org.Vancl.utils.BaseDao;

public class LoginDaoImpl extends BaseDao implements LoginDao {

	public String setCondition(Login login, List<Object> params) {
		if (login == null) {
			return "";
		}
		String sql = "";
		String name =login.getUsername(); 
			if (name != null && !name.equals("")) {
			sql += " and Username =?";
			
			params.add(name);
		}
		
		String pwd=login.getPassword();
		if(pwd != null && !pwd.equals("")) {
			sql += " and Password = ?";
			params.add(pwd);
		}
		return sql;
	}
	
	public Login get(Login login) {
		String sql="select * from Login where 1=1";
		List<Object> params=new ArrayList<Object>();
		sql+=setCondition(login, params);
		ResultSet rs=super.executeQuery(sql, params.toArray());
		Login login2=null;
		try {
			if (rs.next()) {
				login2=new Login();
				login2.setUsername(rs.getString("username"));
				login2.setPassword(rs.getString("password"));
				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				super.CloseAll(rs, rs.getStatement().getConnection(), rs.getStatement());
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return login2;
	}

	@Override
	public int update(Login login) {
		String sql="UPDATE Login SET Password=? WHERE Username=?";
		return super.executeUpdate(sql, login.getPassword(),login.getUsername());
	}

}
